import {
  login,
  getUserInfo
} from '@/api/modules/login'
import {
  getToken,
  setToken,
  removeToken
} from '@/utils/auth'
import {
  password
} from '@/utils/token'
import Vue from 'vue'
const user = {
  state: {
    user: '',
    status: '',
    code: '',
    token: getToken() || "",
    name: '',
    avatar: '',
    introduction: '',
    roles: [],
    setting: {
    articlePlatform: []
    }
  },

  mutations: {
    SET_CODE: (state, code) => {
      state.code = code
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_INTRODUCTION: (state, introduction) => {
      state.introduction = introduction
    },
    // SET_SETTING: (state, setting) => {
    //   state.setting = setting
    // },
    // SET_STATUS: (state, status) => {
    //   state.status = status
    // },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    }
  },
  actions: {
    // 用户名登录
    LoginByUsername({
      commit,
      dispatch
    }, userInfo) {
      // const username = userInfo.phone.trim()
      return new Promise((resolve, reject) => {
        login({
          ...userInfo,
          password:password(userInfo.password)
        }).then(response => {
          // let response= {
          //  data:{
          //   roles: ['admin'],
          //   token: 'admin',
          //   introduction: '我是超级管理员',
          //   avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
          //   name: 'Super Admin'
          //  },
          //  token: 'admin',
          // }
          sessionStorage.setItem('userInfo',JSON.stringify(response.data));
          commit('SET_TOKEN', response.data.token)
          setToken(response.data.token)
          // if (response.data.roles && response.data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            // commit('SET_ROLES', ['admin'])
            dispatch('GenerateRoutes', {
              roles: ['admin']
            })
          // } else {
          //   reject('getInfo: roles must be a non-null array!')
          // }
          Vue.prototype.$unitId = response.data.userInfo.unitId
          commit('SET_NAME', response.data.userInfo.account)
          commit('SET_AVATAR', 'https://spaq.suzhou.gov.cn/ssa_zwtx.png')
          // commit('SET_INTRODUCTION', response.data.introduction)
         
          resolve()
        }).catch(error => {
          console.log(error);
          reject(error)
        })
      })
    },
    // // 获取用户信息
    GetUserInfo({
      commit,
      state,
    }) {
      return new Promise((resolve, reject) => {
        // getUserInfo({}).then(response => {
          // 由于mockjs 不支持自定义状态码只能这样hack
          // if (!response.data) {
          //   reject('Verification failed, please login again.')SET_ROLES
          // }

          // let getToken()
          let response= JSON.parse(sessionStorage.getItem('userInfo'))
          
          // {
          //   data:{
          //    roles: ['admin'],
          //    token: 'admin',
          //    introduction: '我是超级管理员',
          //    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
          //    name: 'Super Admin'
          //   },
          //   token: 'admin',
          //  }
          // const data = response.data
          // if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES',['admin'])
          // } else {
          //   reject('getInfo: roles must be a non-null array!')
          // }
          Vue.prototype.$unitId = response.userInfo.unitId
          commit('SET_NAME',response.userInfo.account)
          commit('SET_AVATAR','https://spaq.suzhou.gov.cn/ssa_zwtx.png')
          // commit('SET_INTRODUCTION', data.introduction)
          resolve(['admin'])
        // }).catch(error => {
        //   console.log(error);
        //   reject(error)
        // })
      })
    },
    // 登出
    LogOut({
      commit,
      state
    }) {
      return new Promise((resolve, reject) => {
        // logout(state.token).then(() => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        console.log(123);
        removeToken()
        resolve()
        // }).catch(error => {
        //   reject(error)
        // })
      })
    },
    // 前端 登出
    FedLogOut({
      commit
    }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    },

    // 动态修改权限(没用)
    ChangeRoles({
      commit,
      dispatch
    }, role) {
      return new Promise(resolve => {
        // commit('SET_TOKEN', role)
        // setToken(role)
        getUserInfo({}).then(response => {
          const data = response.list
          commit('SET_ROLES', data.roles)
          commit('SET_NAME', data.name)
          commit('SET_AVATAR', data.avatar)
          commit('SET_INTRODUCTION', data.introduction)
          dispatch('GenerateRoutes', data) // 动态修改权限后 重绘侧边菜单
          resolve()
        })
      })
    }
  }
}

export default user